package servlet;

import pojo.Booking;
import service.BookingService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/countServlet")
public class countServlet extends HttpServlet {
    BookingService bookingService = new BookingService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        pageNo = (pageNo-1)*pageSize;
        List<Booking> list = BookingService.page(pageNo,pageSize);

        int count = bookingService.count();

        int totalPage = count/pageSize;

        if (count%pageSize!=0){
            totalPage+=1;
        }
        request.setAttribute("list",list);
        request.setAttribute("totalPage",totalPage);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
